import globalData from '../utils/global.js'
import {
    alert,
    toast
} from '../utils/util.js'

let requestList = 0
let errorStatus = false

export class Request {
    constructor(baseurl) {
        this.baseUrl = baseurl
    }
    /**
     * 生成长请求
     * @param {Boolean} state 判断是否显示
     */
    setLongLoading(state){
        requestList += state ? 1: -1
    }
    all(object, config) {
        if (!/http/.test(object.url)) {
            object.url = this.baseUrl + object.url
        }
        if(config.isLoading!==false){
            if (!requestList) {
                wx.showLoading({
                    title: '数据加载中',
                    mask: true
                })
            }
            requestList += 1
        }
        return new Promise((reslove, reject) => {
            object.success = (res) => {
                if (res.statusCode > 300 || res.statusCode < 200 || res.data.code > 300 || res.data
                    .code < 200) {
                    wx.hideLoading({
                        complete() {
                            if (config.isToast !== false) {
                                const msg = res.data.msg ? res.data.msg : '请求错误'
                                if(res?.data?.msg?.length > 20) {
                                    // todo: 事件上报，看未来alert事件多寡决定是否保留
                                    wx.reportEvent('request_alert', {
                                        'click_num': 1,
                                        'request_url': object.url,
                                        'error_msg': msg
                                    })
                                    toast(msg)
                                }
                                else {
                                    toast(msg)
                                }
                            }
                        }
                    })
                    reject(res)
                } else {
                    reslove(res)
                }
            }
            object.fail = (err) => {
                wx.hideLoading()
                if (!errorStatus) {
                    errorStatus = true
                    wx.hideLoading({
                        complete() {
                            if(config.isToast !== false)
                                toast(err.data ? err.data.error : '网络错误，请检查网络后重试')
                        }
                    })
                    errorStatus = false
                }
                reject(err)
            }
            object.complete = () => {
                if(config.isLoading!==false){
                    requestList -= 1
                    if (requestList === 0) {
                        wx.hideLoading()
                    }
                }
            }
            if (globalData.token && object.header)
                object.header.Authorization = 'Wx ' + globalData.token
            if (globalData.token && !object.header)
                object.header = {
                    Authorization: 'Wx ' + globalData.token
                }
            wx.request(object)
        })
    }
    post(url, data, config = {}) {
        return this.all({
            url: url,
            params: config.params || {},
            data: JSON.stringify(data),
            method: 'post',
            header: {
                'content-type': 'application/json'
            }
        }, config)
    }
    get(url, data, config = {}) {
        return this.all({
            url: url,
            data: data,
            method: 'get',
        }, config)
    }
    put(url, data, config = {}) {
        return this.all({
            url: url,
            data: data,
            method: 'put',
        }, config)
    }

}
const request = new Request(globalData.baseUrl)
export default request